* */ class TextReplacement { private $_files; private $_tags; private $_num_replacements; /** * Constructor, takes in files to be processed. * * @param $file_list */ public function __construct($file_list) { $this->_files = $file_list; } /** * Add a tag pattern and it's replacement value and store those as the tags to replace. * * @param $tag * @param $replaceValue */ public function addTag($tag, $replaceValue) { $this->_tags[$tag] = $replaceValue; } /** * Iterates through the files and replaces all the found tags with their replacement values. * * @return bool|int */ public function replaceTags() { $files = $this->_files; $num = 0; foreach ($files as $file) { if (is_file($file)) { $content = file_get_contents($file); } else { return false; } $count = 0; $content = str_replace(array_keys($this->_tags), array_values($this->_tags), $content, $count); $num += $count; $bytes_written = file_put_contents($file, $content); if ($bytes_written === false) { return false; } } $this->_num_replacements = $num; return $num; } /** * Returns the number replacements made. * * @return mixed */ public function getNumberOfReplacements() { return $this->_num_replacements; } } ?>